package cn.donting.jme.expansion.builder.mesh;

import cn.donting.jme.expansion.attribute.Attribute;
import cn.donting.jme.expansion.attribute.ValueChangeListener;
import cn.donting.jme.expansion.attribute.lmpl.BooleanAttribute;
import cn.donting.jme.expansion.attribute.lmpl.FloatAttribute;
import cn.donting.jme.expansion.attribute.lmpl.IntegerAttribute;
import cn.donting.jme.ioc.Bean;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.jme3.scene.shape.Cylinder;
import com.jme3.scene.shape.Sphere;

import javax.swing.*;
import java.util.List;

@Bean
public class CylinderAttributeBuilder implements MeshAttributeBuilder<Cylinder> {
    @Override
    public boolean isHandle(Object o) {
        return o instanceof Cylinder;
    }

    @Override
    public Cylinder newInstance() {
        return new Cylinder(50,100,0.5f,1f,true);
    }

    @Override
    public Icon getIcon(int size) {
        return new FlatSVGIcon("icon/cylinder.svg", size, size);
    }

    @Override
    public void attribute(List<Attribute> attributes, Cylinder mesh) {

        IntegerAttribute axisSamples=new IntegerAttribute("axisSamples: ");
        axisSamples.setMixValue(2);
        axisSamples.setTip("沿轴采样的顶点数。\n" +
                "*等于段数+ 1;所以\n" +
                "*例如，4个样本意味着圆柱体将由3段组成。");
        axisSamples.setValue(mesh.getAxisSamples());
        axisSamples.setValueChangeListener(newValue -> {
            mesh.updateGeometry(newValue,
                    mesh.getRadialSamples(),
                    mesh.getRadius2(),
                    mesh.getRadius(),
                    mesh.getHeight(),
                    mesh.isClosed(),
                    mesh.isInverted()
                    );
            mesh.updateBound();
        });
        attributes.add(axisSamples);

        IntegerAttribute radialSamples=new IntegerAttribute("半径采样: ");
        radialSamples.setMixValue(3);
        radialSamples.setTip("沿半径的三角形样本数。\n" +
                "*例如，4表示正方形的边\n" +
                "*圆柱体由4个矩形组成，顶部和底部由4个三角形组成。");
        radialSamples.setValue(mesh.getRadialSamples());
        radialSamples.setValueChangeListener(newValue -> {
            mesh.updateGeometry(mesh.getAxisSamples(),
                    newValue,
                    mesh.getRadius2(),
                    mesh.getRadius(),
                    mesh.getHeight(),
                    mesh.isClosed(),
                    mesh.isInverted()
            );
            mesh.updateBound();
        });
        attributes.add(radialSamples);

        FloatAttribute topRadius=new FloatAttribute("顶部半径: ");
        topRadius.setMinValue(0.000000001f);
        topRadius.setStepSize(0.1f);
        topRadius.setValue(mesh.getRadius2());
        topRadius.setValueChangeListener(newValue -> {
            mesh.updateGeometry(mesh.getAxisSamples(),
                    mesh.getRadialSamples(),
                    newValue,
                    mesh.getRadius(),
                    mesh.getHeight(),
                    mesh.isClosed(),
                    mesh.isInverted()
            );
            mesh.updateBound();
        });
        attributes.add(topRadius);


        FloatAttribute bottomRadius=new FloatAttribute("底部半径: ");
        bottomRadius.setMinValue(0.000000001f);
        bottomRadius.setStepSize(0.1f);
        bottomRadius.setTip("底部半径");
        bottomRadius.setValue(mesh.getRadius());
        bottomRadius.setValueChangeListener(newValue -> {
            mesh.updateGeometry(mesh.getAxisSamples(),
                    mesh.getRadialSamples(),
                    mesh.getRadius2(),
                    newValue,
                    mesh.getHeight(),
                    mesh.isClosed(),
                    mesh.isInverted()
            );
            mesh.updateBound();
        });
        attributes.add(bottomRadius);


        FloatAttribute height=new FloatAttribute("高度: ");
        height.setMinValue(0.000000001f);
        height.setStepSize(0.1f);
        height.setTip("高度");
        height.setValue(mesh.getHeight());
        height.setValueChangeListener(newValue -> {
            mesh.updateGeometry(mesh.getAxisSamples(),
                    mesh.getRadialSamples(),
                    mesh.getRadius2(),
                    mesh.getRadius(),
                    newValue,
                    mesh.isClosed(),
                    mesh.isInverted()
            );
            mesh.updateBound();
        });
        attributes.add(height);


        BooleanAttribute closed=new BooleanAttribute("封闭: ");
        closed.setValue(mesh.isClosed());
        closed.setTip("封闭顶部和底部");
        closed.setValueChangeListener(newValue -> {
            mesh.updateGeometry(mesh.getAxisSamples(),
                    mesh.getRadialSamples(),
                    mesh.getRadius2(),
                    mesh.getRadius(),
                    mesh.getHeight(),
                    newValue,
                    mesh.isInverted()
            );
            mesh.updateBound();
        });
        attributes.add(closed);


        BooleanAttribute inverted=new BooleanAttribute("inverted: ");
        inverted.setValue(mesh.isInverted());
        inverted.setValueChangeListener(newValue -> {
            mesh.updateGeometry(mesh.getAxisSamples(),
                    mesh.getRadialSamples(),
                    mesh.getRadius2(),
                    mesh.getRadius(),
                    mesh.getHeight(),
                    mesh.isClosed(),
                    newValue
            );
            mesh.updateBound();
        });
        attributes.add(inverted);
    }

    @Override
    public String getName() {
        return "圆柱体";
    }
}
